package com.yy.controller.admin;

import com.yy.dto.CategoryDTO;
import com.yy.dto.CategoryPageQueryDTO;
import com.yy.entity.Category;
import com.yy.result.PageResult;
import com.yy.result.Result;
import com.yy.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PutMapping
    public Result update(@RequestBody CategoryDTO dto) {
        categoryService.update(dto);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(CategoryPageQueryDTO dto) {
        PageResult pageResult = categoryService.list(dto);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        categoryService.enableOrDisable(status, id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody CategoryDTO dto){
        categoryService.add(dto);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Long id){
        categoryService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getByType(Integer type){
        List<Category> categoryList = categoryService.getByType(type);
        return  Result.success(categoryList);
    }
}
